package com.cat.chapter038;

import java.util.ArrayList;
import java.util.List;


/**
 * @author cat
 * @description https://leetcode.cn/problems/letter-case-permutation/
 * @create 2025/7/8 08:37
 * @since JDK17
 */

public class Solution04 {
    List<String> answer = new ArrayList<>();
    StringBuilder sb;
    public List<String> letterCasePermutation(String s) {
        char[] array = s.toCharArray();
        sb = new StringBuilder(s);
        f(array, 0, array.length);
        return answer;
    }

    public void f(char[] arr, int cur, int n) {
        if (cur == n) {
            answer.add(sb.toString());
            return;
        }
        // 不管是否为字母都不改
        f(arr, cur + 1, n);
        if (Character.isLetter(arr[cur])) { // 当前字符是一个字母
            // 能改并且改
            char c = sb.charAt(cur);
            sb.setCharAt(cur, g(c));
            f(arr, cur + 1, n);
            sb.setCharAt(cur, g(c));
        }

    }
    public char g(char c) { // 大小写转换
        if (Character.isUpperCase(c)) {
            return ((char) (c + 32));
        } else {
            return ((char) (c - 32));
        }
    }
}
